package practice;

public class MyList implements TheList {
    ListNode head;//头节点
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head==null){
            head = node;
        }else{
            ListNode cur = head;
            while (cur.next!=null){
                cur = cur.next;
            }
            cur.next = node;
        }

    }

    @Override
    public void addIndex(int index, int data) throws NullListException,IndexException{
        if(head==null){
            throw new NullListException("警告:链表为空!");
        }
        if (index<0||index>size()){
            throw new IndexException("警告:index不合法!");
        }
        if(index==0){
            addFirst(data);
        }
        if (index==size()){
            addLast(data);
        }

        //中间
        ListNode cur = KeyNode(index);
        ListNode node = new ListNode(data);
        node.next= cur.next;
        cur.next = node;

    }

    public ListNode KeyNode(int index){
        ListNode cur = head;
        int flag = 0;
        while (flag!=index-1) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur!=null){
            if (cur.val ==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) throws NullListException{
        //只移掉第一个
        if(head==null){
            throw new NullListException("警告:链表为空!");
        }
        if(head.val == key){
            head=head.next;
        }
        ListNode cur = getKey(key);
        cur.next = cur.next.next;
    }

    public ListNode getKey(int data){
        ListNode cur =head;
        while (cur.next!=null){
            if (cur.next.val==data){
                break;
            }
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public ListNode removeAllKey(int key) throws NullListException{
        if(head==null){
            throw new NullListException("警告:链表为空!");
        }
        while (head!=null){
            if(head.val!=key){
                break;
            }
            head = head.next;
            if(head==null){
                return head;
            }
        }

        //删掉所有与关键字相同的结点
        ListNode prev = head;
        ListNode cur = head.next;

        while (cur!=null){
            if(cur.val ==key){
                prev.next = cur.next;
                cur = cur.next;
            }
            else {
                prev = cur;
                cur = cur.next;
            }
        }
        return head;
    }

    @Override
    public int size() {
        int flag = 0;
        ListNode cur  = head;
        while(cur!=null){
            flag++;
            cur = cur.next;
        }
        return flag;
    }

    @Override
    public void display() throws NullListException{
        if (head==null){
            throw new NullListException("警告:链表为空!");
        }
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
}